<?php
/**
 * @package: Asalsuri
 * @subpackage: Admin Model
 * @author: Truong Kim Phung - Pham Van Diep
 * @date: January 24, 2014 - January 27, 2014
 * 
*/
class Club_Model extends CI_Model{
	var $database;
	public function __construct() {
        parent::__construct();
		$this->database = $this->db->conn_id;
		
	}
    /**
     * @author: Truong Kim Phung
     * @description: create object admin
     * @param array $row: array admin 
     * @return object | null
    */
	private function _clubToObj($row) {
		if(isset($row)) {
			$objClub			    = new stdClass();
			$objClub->id 	    	= isset($row['clubid'])      ? $row['clubid']  : 0;
			$objClub->name          = isset($row['name'])        ? $row['name']    : '';
			$objClub->name_se       = isset($row['name_se'])     ? $row['name_se'] : '';
            $objClub->nickname      = isset($row['nickname'])    ? $row['nickname']: '';
            $objClub->country       = isset($row['country'])     ? $row['country'] : '';
            $objClub->logo          = isset($row['logo'])        ? $row['logo']    : '';
            $objClub->slug          = isset($row['slug'])        ? $row['slug']    : '';
			$objClub->short 	    = isset($row['short'])       ? $row['short']   : '';
			$objClub->description	= isset($row['description']) ? $row['description'] : '';
            $objClub->tag           = isset($row['tag'])         ? $row['tag']     : '';
            $objClub->created       = isset($row['created'])     ? $row['created'] : '';
            $objClub->modified      = isset($row['modified'])    ? $row['modified']: '';
            $objClub->logid         = isset($row['logid'])       ? $row['logid']   : 0;
            $objClub->status        = isset($row['status'])      ? $row['status']  : 0;
            $objClub->co_post       = isset($row['co_post'])     ? $row['co_post'] : 0;
            $objClub->co_fan        = isset($row['co_fan'])      ? $row['co_fan']  : 0;
            $objClub->co_like       = isset($row['co_like'])     ? $row['co_like'] : 0;
            $objClub->co_follow     = isset($row['co_follow'])   ? $row['co_follow'] : 0;
            $objClub->co_sponsor    = isset($row['co_sponsor'])  ? $row['co_sponsor'] : 0;
			return $objClub;
		}
		return null;
	}
    /**
     * @author: Truong Kim Phung
     * @description: get club info
     * @param integer $intId: ID
     * @return object | null
    */
	public function get($intId) {	
		$stmt = $this->database->prepare('CALL clubs_get(?)');
		$stmt->bindParam(1, $intId, PDO::PARAM_INT, 11);
		$stmt->execute();
		return  $this->_clubToObj($stmt->fetch());
        //return $stmt->fetchAll();			
	}
    /**
    * @author: Pham Van Diep
    * @final: Truong Kim Phung
    * @description: get all clubs
    * @param integer $intLoginid: user login id
    * @param string $strName: name of club
    * @param string $strNickname: nickname of club
    * @param string $strCountry: name country
    * @param string $strLogo: path of logo image 
    * @param string $strSlug: slug
    * @param string $strShort: string short description
    * @param string $strDescription: string description
    * @param string $strTag: string tag  
    * @param array
    */
    public function add($intLoginid, $strName, $strNickname='', $strCountry='', $strLogo='', $strSlug='', $strShort='', $strDescription='', $strTag='') {
		$stmt = $this->database->prepare('CALL clubs_add(?,?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginid,        PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $strName,           PDO::PARAM_STR, 150);
		$stmt->bindParam(3, $strNickname,       PDO::PARAM_STR, 150);
		$stmt->bindParam(4, $strCountry,        PDO::PARAM_STR, 2);
		$stmt->bindParam(5, $strLogo,           PDO::PARAM_STR, 150);
        $stmt->bindParam(6, $strSlug,           PDO::PARAM_STR, 255);
        $stmt->bindParam(7, $strShort,          PDO::PARAM_STR, 255);
        $stmt->bindParam(8, $strDescription,    PDO::PARAM_STR);
        $stmt->bindParam(9, $strTag,            PDO::PARAM_STR, 100);					
		$stmt->execute();	
        $result = $stmt->fetch();
        return isset($result['errCode']) ? $result['errCode'] : -1;	
	}    
    /**
    * @author: Pham Van Diep
    * @final: Truong Kim Phung
    * @description: get all clubs
    * @param integer $intLoginid: user login id
    * @param integer $intStatus: status
    * @param integer $intOption: option : 0-> all
    * @param string $strKeyword 
    * @param datetime $dStart : start date
    * @param datetime $dEnd : end date 
    * @param integer  $intOffset: start of paging
    * @param integer  $intLimit: paging number 
    * @param array
    */
    public function getAll($intLoginid,$intStatus,$intOption,$strKeyword,$dStart,$dEnd,$intOffset,$intLimit) {
		$stmt = $this->database->prepare('CALL clubs_getall_a(?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginid,     PDO::PARAM_INT);
		$stmt->bindParam(2, $intStatus,      PDO::PARAM_INT);
		$stmt->bindParam(3, $intOption,      PDO::PARAM_INT, 4);
        $stmt->bindParam(4, $strKeyword,     PDO::PARAM_STR, 50);
        $stmt->bindParam(5, $dStart,         PDO::PARAM_STR);
        $stmt->bindParam(6, $dEnd,           PDO::PARAM_STR);
        $stmt->bindParam(7, $intOffset,       PDO::PARAM_INT);
        $stmt->bindParam(8, $intLimit,       PDO::PARAM_INT, 6);	
		$stmt->execute();
		$result = $stmt->fetchAll();        
		$intTotal = isset($result[0]['foundRows']) ? $result[0]['foundRows'] : 0;
		return isset($result) ? $result : null;
	}
    /**
    * @author: Pham Van Diep
    * @final: Truong Kim Phung
    * @description: delete club
    * @param integer $intLoginid: user login id
    * @param integer $intClubid: club id     
    * @param integer
    */
    public function delete($intLoginid,$intClubid){
        $stmt = $this->database->prepare('CALL clubs_delete(?,?)');
		$stmt->bindParam(1, $intLoginid, PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intClubid,  PDO::PARAM_INT, 11);
		$stmt->execute();
        $result = $stmt->fetch();
        return isset($result['errCode']) ? $result['errCode'] : -1;			
    }
    /**
    * @author: Pham Van Diep
    * @final: Truong Kim Phung
    * @description: update club infomation
    * @param integer $intLoginid: user login id
    * @param string $strName: name of club
    * @param string $strNickname: nickname of club
    * @param string $strCountry: name country
    * @param string $strLogo: path of logo image 
    * @param string $strSlug: slug
    * @param string $strShort: string short description
    * @param string $strDescription: string description
    * @param string $strTag: string tag  
    * @param integer
    */
    public function update($intPloginid,$intClubid, $strPname, $strPnickname='', $strPcountry='', $strPlogo='', $strPslug='', $strPshort='', $strPdescription='', $strPtag='') {
		$stmt = $this->database->prepare('CALL clubs_update(?,?,?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intPloginid,         PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intClubid,           PDO::PARAM_INT, 11);
		$stmt->bindParam(3, $strPname,            PDO::PARAM_STR, 150);
		$stmt->bindParam(4, $strPnickname,        PDO::PARAM_STR, 150);
		$stmt->bindParam(5, $strPcountry,         PDO::PARAM_STR, 2);
		$stmt->bindParam(6, $strPlogo,            PDO::PARAM_STR, 150);
        $stmt->bindParam(7, $strPslug,            PDO::PARAM_STR, 255);
        $stmt->bindParam(8, $strPshort,           PDO::PARAM_STR, 255);
        $stmt->bindParam(9, $strPdescription,     PDO::PARAM_STR);
        $stmt->bindParam(10, $strPtag,            PDO::PARAM_STR, 100);					
		$stmt->execute();	
        $result = $stmt->fetch();
        return isset($result['errCode']) ? $result['errCode'] : -1;	
	}
    /**
    * @author: Pham Van Diep
    * @final: Truong Kim Phung
    * @description: update status of club
    * @param integer $intLoginid: user login id
    * @param integer $intClubid: club id
    * @param integer $intStatus: club status
    * @param integer $intPerm: permission      
    * @param integer
    */
    public function updateStatus($intLoginid,$intClubid,$intStatus,$intPerm){
        $stmt = $this->database->prepare('CALL clubs_update_status(?,?,?,?)');
		$stmt->bindParam(1, $intLoginid,  PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intClubid,  PDO::PARAM_INT, 11);
		$stmt->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
		$stmt->bindParam(4, $intPerm, PDO::PARAM_INT, 4);							
		$stmt->execute();	
        $result = $stmt->fetch();
        return isset($result['errCode']) ? $result['errCode'] : -1;	
    }	
	/**
	*	@description: get Error code from query result
	*	@param: mixed query result
	*	@return: int
	*/
	public function getErrorCode ( $tableData ) {
		return isset($tableData['errCode'])?intval($tableData['errCode']):0;
	}	
	
}